package StudyCode.summary.myfile;

import java.io.*;
import java.nio.ByteBuffer;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class DiffFileAndByteBuffer {
	public static boolean diff(File f1, ByteBuffer b2) throws IOException {
		if (f1 != null && !f1.exists())
			f1 = null;

		if (b2 != null)
			b2 = b2.slice();

		if (f1 == null)
			return b2 != null && b2.remaining() != 0;

		if (b2 == null)
			return f1 != null && f1.length() != 0;

		if (f1.length() != b2.remaining())
			return true;

		DataInputStream s1 = new DataInputStream(new BufferedInputStream(
				new FileInputStream(f1)));
		final int len = b2.capacity();
		int p = 0;

		for (; p < len - 7; p += 8) {
			if (s1.readLong() != b2.getLong())
				return true;
		}

		for (; p < len; p++) {
			if (s1.read() != (b2.get() & 0xFF))
				return true;
		}

		s1.close();
		return false;
	}
}
